﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AUV5.Model;
using AUV5.BLL;

namespace AUV5.Web.Pages
{
    public partial class Password : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["uid"] != null && Request.QueryString["s"] != null)
                {
                    string uid = Request.QueryString["uid"];

                    UserInfo user = UserInfoBLL.Instance.GetDetail(uid);

                    if (user != null)
                    {
                        ActivationCode code = ActivationCodeBLL.Instance.GetCodeByUserIDAndStatus(uid, (int)Common.Enum.ActivationCodeStatus.ForgetPassword);
                        if (code != null)
                        {
                            string secret = Request.QueryString["s"];
                            string getSecret =BLL.UserInfoBLL.Instance.GetRetrieveKey(user.UserID,code.Code);
                            if (secret == getSecret)
                            {
                                ShowDiv(true);
                            }
                            else
                            {
                                //AlertGo("抱歉，该链接无效", "Index.aspx");
                                Response.Redirect(URL_INDEX);
                            }
                        }
                        else
                        {
                            //Response.Redirect(URL_INDEX);
                            Response.Write("<script language='javascript'>alert('抱歉，该链接已经失效');location.href='"+URL_INDEX+"';</script>");
                            //AlertGo("抱歉，该链接已经失效", "Index.aspx");
                        }
                    }
                    else
                    {
                        //AlertGo("您无权访问该页", "Index.aspx");
                        Response.Redirect(URL_INDEX);
                    }
                }
                else
                {
                    ShowDiv(false);
                }
            }
        }

        //发送找回密码的验证码
        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (txbResult.Text.Trim() != Convert.ToString(Session["MathIdentifyResult"]))
            {
                this.lblMessage.Text = "数学题算错了";
                txbResult.Text = "";
                return;
            }


            AUV5.Model.UserInfo user = new UserInfo();
            //用户名
            user = AUV5.BLL.UserInfoBLL.Instance.GetDetailByName(txtUserName.Text.Trim());
            //或邮箱
            if (user == null)
            {
                user = BLL.UserInfoBLL.Instance.GetDetailByEmail(txtUserName.Text.Trim());
            }
            if (user != null)
            {
                string userID = user.UserID;

                //发送邮件 
                BLL.UserInfoBLL.Instance.SendRetrievePasswordEmail(user);

                string emailShort = GetShortOfEmail(user.Email);

                AlertGo("邮件已经发送到您" + emailShort + "的邮箱，请注意查收", URL_INDEX);
            }
            else
            {
                txtUserName.Text = "";
                Alert("该用户不存在");
            }
        }

        //控制两层的显示
        void ShowDiv(bool isChang)
        {
            if (isChang)
            {
                divChange.Attributes["style"] = "display:block";
                divUserName.Attributes["style"] = "display:none";
            }
            else
            {
                divChange.Attributes["style"] = "display:none";
                divUserName.Attributes["style"] = "display:block";
            }
        }
        //修改密码
        protected void btnChange_Click(object sender, EventArgs e)
        {
            //原则上没有取到uid的话，不会显示该按钮
            if (Request.QueryString["uid"] != null)
            {
                string uid = Request.QueryString["uid"];
                UserInfo user = UserInfoBLL.Instance.GetDetail(uid);
                user.Password = base.EncryptMD5(txtRePassword.Text.Trim());
                UserInfoBLL.Instance.Update(user);
                //修改密码后删除Code
                ActivationCode code = BLL.ActivationCodeBLL.Instance.GetCodeByUserIDAndStatus(uid, (int)Common.Enum.ActivationCodeStatus.ForgetPassword);
                BLL.ActivationCodeBLL.Instance.Delete(code);
                AlertGo("修改成功，请登录", URL_LOGIN);
            }
            //else
            //    Alert("没取到uid");
        }
        //获取email的隐蔽型字符串
        string GetShortOfEmail(string email)
        {
            if (email != null)
            {
                string[] strArr = email.Split('@');
                string firstStr = email.Substring(0, 2);
                System.Text.StringBuilder secondStr = new System.Text.StringBuilder();

                string firstEle = strArr[0];
                int length = firstEle.Length;

                secondStr.Append(firstEle.Substring(length - 1));
                secondStr.Append("@");
                secondStr.Append(strArr[1]);

                return firstStr + "*********" + secondStr.ToString();
            }
            else
                return "";
        }
    }
}